<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>开班对切完成情况统计</title>
    <style>
        table {
            width: 100%;
            border-collapse: separate;
            border-spacing: 0;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 0 20px rgba(0,0,0,0.15);
        }
        th, td {
            padding: 12px 15px;
            text-align: center;
            border-bottom: 1px solid #dddddd;
            transition: all 0.3s ease;
        }
        th {
            background-color: #009879;
            color: #ffffff;
            font-weight: bold;
        }
        tr:hover {
            background-color: #ffeb3b;
            transform: scale(1.05);
            box-shadow: 0 0 10px rgba(0,0,0,0.3);
            font-weight: bold;
            transition: all 0.3s ease;
        }
        tr:nth-child(even) {
            background-color: #f3f3f3;
        }
        .disabled {
            position: relative;
            color: #999;
            pointer-events: none;
        }
        .disabled::after {
            content: '';
            position: absolute;
            left: 0;
            right: 0;
            top: 50%;
            height: 2px;
            background-color: #999;
            transform: rotate(0deg);
        }
        tr.disabled td {
            text-decoration: line-through;
            color: #999;
        }
        h1 {
            color: #009879;
            margin-bottom: 30px;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>开班对切完成情况统计</h1>
    <table>
        <thead>
            <tr class="${item.enabled ? '' : 'disabled'}">
                <th>序号</th>
                <th>线别</th>
                <th>对切完成情况</th>
                <th>开班FF</th>
                <th>开班UOC</th>
                <th>开班iSC</th>
                <th>当前FF</th>
                <th>当前UOC</th>
                <th>当前ISC</th>
                <th>影响效率</th>
            </tr>
        </thead>
        <tbody id="data-body">
            <!-- 数据将动态插入 -->
        </tbody>
    </table>
    <script>
        // 添加排序功能
        const sortTable = (columnIndex, order) => {
            const table = document.querySelector('table');
            const tbody = table.querySelector('tbody');
            const rows = Array.from(tbody.querySelectorAll('tr'));

            rows.sort((a, b) => {
                const aValue = a.querySelectorAll('td')[columnIndex].textContent;
                const bValue = b.querySelectorAll('td')[columnIndex].textContent;

                // 尝试将值转换为数字
                const numA = parseFloat(aValue);
                const numB = parseFloat(bValue);

                // 如果两个值都是有效数字，则按数字排序
                if (!isNaN(numA) && !isNaN(numB)) {
                    return order === 'asc' ? numA - numB : numB - numA;
                }
                // 否则按字符串排序
                else {
                    return order === 'asc' ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
                }
            });

            tbody.innerHTML = '';
            rows.forEach(row => tbody.appendChild(row));
        };


        // 为表头添加点击事件
        const headers = document.querySelectorAll('th');
        headers.forEach((header, index) => {
            header.addEventListener('click', () => {
                const currentOrder = header.dataset.order || 'asc';
                sortTable(index, currentOrder);
                header.dataset.order = currentOrder === 'asc' ? 'desc' : 'asc';
            });
        });
    async function fetchData() {
        try {
            const response = await fetch('/api/ipconfig');
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            const data = await response.json();
            console.log('获取到的数据:', data);
            renderTable(data);
        } catch (error) {
            console.error('获取数据失败:', error);
            alert('数据加载失败，请检查网络连接并确保后端服务正在运行');
        }
    }

    function renderTable(data) {
        const tbody = document.getElementById('data-body');
        tbody.innerHTML = data.map((item, index) => {
            const grade = item.active_line_grade || 'empty';
            const circleColor = item.cutover_completed === true ? 'green' : 'red';
            return `
                <tr class="${item.enabled ? '' : 'disabled'}">
                    <td>${index + 1}</td>
                    <td>${item.line}</td>
                    <td><div style="width: 20px; height: 20px; background-color: ${circleColor}; border-radius: 50%; margin: 0 auto;"></div></td>
                    <td class="${item.enabled ? '' : 'disabled'}">${parseFloat(item.opening_ff).toFixed(2)}</td>
                    <td class="${item.enabled ? '' : 'disabled'}">${parseFloat(item.opening_uoc).toFixed(2)}</td>
                    <td class="${item.enabled ? '' : 'disabled'}">${parseFloat(item.opening_isc).toFixed(3)}</td>
                    <td class="${item.enabled ? '' : 'disabled'}">${parseFloat(item.current_ff).toFixed(2)}</td>
                    <td class="${item.enabled ? '' : 'disabled'}">${parseFloat(item.current_uoc).toFixed(2)}</td>
                    <td class="${item.enabled ? '' : 'disabled'}">${parseFloat(item.current_isc).toFixed(3)}</td>
                    <td style="${Math.abs(item.efficiency_impact) > 0.05 ? 'font-weight: bold; color: red;' : ''}">${parseFloat(item.efficiency_impact).toFixed(3)}</td>
                </tr>
            `;
        }).join('');
    }

    document.addEventListener('DOMContentLoaded', fetchData);
    setInterval(fetchData, 600000); // 每分钟自动刷新数据
</script>
</body>
</html>